import greenfoot.*;

/**
 * This class defines a crab. Crabs live on the beach.
 */
public class Crab extends Actor
{
    GreenfootImage image1 = new GreenfootImage("crab.png");
    GreenfootImage image2 = new GreenfootImage("crab2.png");
    int count = 0;
    
    public Crab( ){
        setImage(image1);
    }
    
    public void animation( ){
        if (getImage( ) == image1) {
            setImage(image2);
        } else {
            setImage(image1);
        }
    }
    
    public void act( ){
          move(5);
          turnAtEdge( );
          control( );
          eat( );
          animation( );
    }
    
    public void eat( ){
        if(isTouching(Worm.class)){
           removeTouching(Worm.class);
           count++;
           CrabWorld w = (CrabWorld)getWorld( );
           w.showScore(count);
           w.drawNum(count);
        }
    }
    
    public void turnAtEdge( ){
        if (isAtEdge( )) {
            turn(18);
        }
    }
    
    public void control( ){         
        if (Greenfoot.isKeyDown("left")) {
            turn(-18);
        } 
        if (Greenfoot.isKeyDown("right")) {
            turn(18);
        }
    }
}